/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m006.ui.views;

import com.vaadin.data.Container;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;
import ep.libs.implementations.AbstractModule;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m005.ui.views.M005View;

/**
 *
 * @author Spyros
 */
public class PrescFinalReadOnly extends M005View {

    public PrescFinalReadOnly(AbstractModule module) {
        super(module);
    }

    public PrescFinalReadOnly(AbstractModule module, Object[] args) {
        super(module, args);
    }

    @Override
    protected void initilizeComponents() {
        super.initilizeComponents();
        prescForm.setReadOnly(true);
        dosageEditableTable.getRemoveButton().setVisible(false);
        dosageEditableTable.getAddButton().setVisible(false);
        submitButton.setCaption("Επανάληψη Συνταγής");
    }

    @Override
    protected void initializeListeners() {
        backButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                changeModule();
            }
        });
        submitButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    prescForm.commit();
                    dosageEditableTable.getTable().commit();
                    boolean saveOk = save(true);
                    if (saveOk) {
                        changeModule();
                        module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.DOCTOR_TYPE, patientObject});
                    }
                } catch (InvalidValueException invalidValueException) {
                }
            }
        });
    }

    @Override
    protected void changeModule(){
        module.getApplication().getMainWindow().removeWindow(submitButton.getWindow());
    }

    @Override
    protected TableFieldFactory initializeTableFieldFactory() {
        final Table temp = new Table();
        TableFieldFactory newTableFieldFactory = new TableFieldFactory() {

            @Override
            public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
                Field f = temp.getTableFieldFactory().createField(container, itemId, propertyId, uiContext);
                f.setReadOnly(true);
                return f;
            }
        };

        return newTableFieldFactory;
    }
}
